package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.feign.WmUserFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    public static final short PASS_AUTH=9;
    public static final short FAIL_AUTH=2;

    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private WmUserFeign wmUserFeign;
    @Autowired
    private ApAuthorFeign apAuthorFeign;

    @Override
    public PageResponseResult listByStatus(AuthDto authDto) {
        //判断参数是否为空
        if (authDto==null){
            return (PageResponseResult) ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查参数
        authDto.checkParam();

        QueryWrapper<ApUserRealname> queryWrapper=new QueryWrapper<>();
        if (authDto.getStatus()!=null){
            queryWrapper.lambda().eq(ApUserRealname::getStatus,authDto.getStatus());
        }
        Page pageParam=new Page(authDto.getPage(),authDto.getSize());
        IPage page = page(pageParam, queryWrapper);
        PageResponseResult pageResponseResult=new PageResponseResult(authDto.getPage(),authDto.getSize(),(int)page.getTotal());

        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    /*
    * 普通用户申请成为自媒体用户
    *
    * */
    @GlobalTransactional
    @Override
    public ResponseResult updateStatusById(AuthDto authDto, Short status) {
        //1.参数校验
        if (authDto==null || status==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (status!=9 && status!=2){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //更新ApUserRealname表
        ApUserRealname apUserRealname=new ApUserRealname();
        apUserRealname.setId(authDto.getId());
        apUserRealname.setStatus(status);
        if (authDto.getMsg()!=null){
            apUserRealname.setReason(authDto.getMsg());
        }
        apUserRealname.setUpdatedTime(new Date());
        this.updateById(apUserRealname);
        //2.判断审核状态
        if (status.equals(PASS_AUTH)){
            ResponseResult result = this.createWmUserAndAuthor(authDto);
            if (result.getCode()!=0){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
        }
        //3.更新apuser表
        ApUser apUser=new ApUser();
        apUser.setId(authDto.getId());
        apUser.setFlag(Short.valueOf("1"));//强转
        apUserMapper.updateById(apUser);



        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    //保存wmuser 和apauthor表数据
    private ResponseResult createWmUserAndAuthor(AuthDto authDto){

        //3.远程调wemedia query

        //根据apuserrealname userid 查询 apuser表 主键
        ApUser apUser = apUserMapper.selectById(authDto.getId());
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //根据name查询 wmuser表
        WmUser wmUser = wmUserFeign.findByName(apUser.getName());
        if (wmUser==null){
            //4.false save到自媒体
            wmUser = new WmUser();
            //设置ApUserId
            wmUser.setApUserId(apUser.getId());
            wmUser.setCreatedTime(new Date());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setStatus(9);//审核状态通过
            wmUser.setPhone(apUser.getPhone());
            wmUserFeign.add(wmUser);

        }

        //5.作者表
        Integer apUserId = apUser.getId();
        ApAuthor apAuthor = apAuthorFeign.findByUserId(apUserId);
        if (apAuthor==null){
            apAuthor = new ApAuthor();
            apAuthor.setName(wmUser.getName());
            apAuthor.setType(2);
            apAuthor.setCreatedTime(new Date());
            apAuthor.setUserId(apUserId);
            apAuthor.setWmUserId(wmUser.getId());
            apAuthorFeign.saveApAuthor(apAuthor);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
